package com.yiqi.liebang.feature.mine.view.adapter;

import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.utils.T;
import com.yiqi.liebang.R;
import com.yiqi.liebang.common.widget.dialog.NormalDialog;
import com.yiqi.liebang.common.widget.dialog.OnBtnClickL;
import com.yiqi.liebang.entity.bo.CommentBo;
import com.yiqi.liebang.entity.bo.UserCenterBo;
import com.yiqi.liebang.framework.api.UserApi;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class ProfessionalAdapter extends BaseQuickAdapter<UserCenterBo.UserClassifyBean, BaseViewHolder> {
    private String userHead;
    boolean ismy;
    private int isComment;
    public ProfessionalAdapter(String userHead, boolean ismy,int isComment) {
        super(R.layout.item_userinfo_zhiyebq);
        this.userHead = userHead;
        this.ismy = ismy;
        this.isComment=isComment;
    }

    @Override
    protected void convert(BaseViewHolder helper, UserCenterBo.UserClassifyBean item) {
        helper.setText(R.id.tv_clasfiy_name, item.getClassify())
                .setText(R.id.tv_clasfiy_num, item.getAcceptNum() + "人认可")
                .setImageResource(R.id.tv_comment_zan, item.getClassifyIsRecord() == 1 ? R.drawable.icon_dianzan_sel : R.drawable.btn_dianzan)
//                .setVisible(R.id.tv_comment_zan, !ismy)
        ;

        if (ismy) {
            helper.getView(R.id.tv_comment_zan).setVisibility(View.GONE);
        } else {
            helper.getView(R.id.tv_comment_zan).setVisibility(View.VISIBLE);
        }
        AM.image().bindToCircleObject(item.getUserHead(), helper.getView(R.id.iv_clasfiy_user));
        CommentBo commentBo = new CommentBo();
        commentBo.setClassifyId(item.getId());
        helper.getView(R.id.tv_comment_zan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isComment==1){
                    AM.api().createApiService(UserApi.class)
                            .zanLabel(commentBo)
                            .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                            .subscribe(new Observer<String>() {
                                @Override
                                public void onSubscribe(Disposable d) {

                                }

                                @Override
                                public void onNext(String s) {
                                    T.showShort("已点赞！");
                                    helper.setText(R.id.tv_clasfiy_num, (item.getAcceptNum() + 1) + "人认可");
                                    AM.image().bindToCircleObject(R.drawable.icon_dianzan_sel, helper.getView(R.id.tv_comment_zan));
                                }

                                @Override
                                public void onError(Throwable e) {
                                    T.showShort(e.getMessage());
                                }

                                @Override
                                public void onComplete() {

                                }
                            });
                }else {
                    final NormalDialog dialog = new NormalDialog(mContext);
                    dialog.content("您暂无权限点赞该用户！")//
                            .btnNum(1)
                            .btnText("取消")//
                            .show();

                    dialog.setOnBtnClickL(new OnBtnClickL() {
                        @Override
                        public void onBtnClick() {
                            dialog.dismiss();
                        }
                    });
                }

            }
        });

    }
}
